#! /usr/bin/perl
######################################################################
# Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
# All Rights Reserved
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Author nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
################################################################################
#
# genconfig (generate a *-config script)
# Peter J Jones (pjones@pmade.org)
#
# This file is part of cxxtools (http://pmade.org/pjones/software/cxxtools/)
#
################################################################################
#
# Includes
#
################################################################################
use strict;
use Getopt::Long;
################################################################################
#
# Constants
#
################################################################################
use constant DATE   => 'Tue Jan 15 16:14:25 PST 2002';
use constant ID	    => '$Id: genconfig,v 1.1 2003/08/12 06:28:53 jason Exp $';
################################################################################
#
# Global Variables
#
################################################################################
my %clo;
my $now = localtime;
################################################################################
#
# Code Start
#
################################################################################
GetOptions(
    \%clo,

    'help|h!',
    'output|o=s',
    'version=s',
    'libs=s',
    'cxxflags=s',
    'name=s',
    'desc=s',
    'pkgconfig=s',
    'prefix=s',
    'bindir=s',
    'libdir=s',
    'incdir=s',
) || usage();
$clo{'help'} && usage();

sub usage {
    print "Usage: $0 [options]\n", <<EOT;
  -h, --help          This message
  -o, --output file   The file to place the output
  --version string    The version string
  --libs string       Encoded libs string
  --cxxflags string   Encoded cxxflags string
  --name name         The name of your program
  --desc text         Short description of the program
  --pkgconfig file    Generate a .pc file.

  --prefix path       Path prefix
  --bindir path       Path to bin dir
  --libdir path       Path to lib dir
  --incdir path       Path to include dir

  Strings that start with a dash should be encoded.
  To encode a string, change the dash to a caret (^).
EOT
    exit 1;
}

if (!open(OUT, ">$clo{'output'}")) {
    print STDERR "$0: can't open file $clo{'output'}: $!\n";
    exit 1;
}

select(OUT);

$clo{'version'}	    ||= '0.1';
$clo{'libs'}	    ||= '';
$clo{'cxxflags'}    ||= '';
$clo{'name'}	    ||= '';
$clo{'desc'}	    ||= '';
$clo{'pkgconfig'}   ||= '';

$clo{'prefix'}	    ||= '/usr/local';
$clo{'bindir'}	    ||= "$clo{'prefix'}/bin";
$clo{'libdir'}	    ||= "$clo{'prefix'}/lib";
$clo{'incdir'}	    ||= "$clo{'prefix'}/include";

($clo{'outfile'} = $clo{'output'}) =~ s/^.*\///;
s/\^/-/g foreach ($clo{'cxxflags'}, $clo{'libs'});

print <<EOT;
#! /bin/sh

##
# This file was auto-generated by genconfig on $now
##

usage()
{
    cat <<EOH
Usage: $clo{'outfile'} [options]
  --version   print the version number for $clo{'name'}
  --libs      print the list of libraries you must use
  --cxxflags  print the C++ compiler flags to use
EOH

    exit 1
}

if test \$# -eq 0; then
    usage
fi

while test \$# -gt 0; do
    case "\$1" in
	--version)
	    echo "$clo{'version'}"
	    exit 0
	    ;;

	--libs)
	    echo "$clo{'libs'}"
	    ;;

        --cflags)
	    echo "$clo{'cxxflags'}"
	    ;;

	--cxxflags)
	    echo "$clo{'cxxflags'}"
	    ;;

	*)
	    usage
	    ;;

    esac
    shift
done

exit 0
EOT

close(OUT);
chmod(0755, $clo{'output'});

if ($clo{'pkgconfig'}) {
    if (!open(PC, ">$clo{'pkgconfig'}")) { die $clo{'pkgconfig'}; }
    select(PC);
    print <<EOT;
prefix=$clo{'prefix'}
exec_prefix=$clo{'bindir'}
libdir=$clo{'libdir'}
includedir=$clo{'incdir'}

Name: $clo{'name'}
Version: $clo{'version'}
Description: $clo{'desc'}
Requires:
Libs: $clo{'libs'}
Cflags: $clo{'cxxflags'}
EOT
    close(PC);
    chmod(0644, $clo{'pkgconfig'});
}
